package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeInfoBo;
import org.dromara.knowledge.domain.vo.KnowledgeInfoVo;
import org.dromara.knowledge.domain.vo.KnowledgeSearchResultVo;
import org.dromara.knowledge.domain.vo.KnowledgeStatusVo;
import org.dromara.knowledge.service.KnowledgeInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 知识库信息控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/info")
public class KnowledgeInfoController extends BaseController {

    private final KnowledgeInfoService knowledgeInfoService;

    /**
     * 查询知识库信息列表
     */
    @SaCheckPermission("knowledge:info:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeInfoVo> list(KnowledgeInfoBo bo, PageQuery pageQuery) {
        return knowledgeInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库信息列表
     */
    @SaCheckPermission("knowledge:info:export")
    @Log(title = "知识库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeInfoBo bo, HttpServletResponse response) {
        List<KnowledgeInfoVo> list = knowledgeInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库信息数据", KnowledgeInfoVo.class, response);
    }

    /**
     * 获取知识库信息详细信息
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:info:query")
    @GetMapping("/{knowledgeId}")
    public R<KnowledgeInfoVo> getInfo(@PathVariable Long knowledgeId) {
        return R.ok(knowledgeInfoService.queryById(knowledgeId));
    }

    /**
     * 新增知识库信息
     */
    @SaCheckPermission("knowledge:info:add")
    @Log(title = "知识库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeInfoBo bo) {
        return toAjax(knowledgeInfoService.insertByBo(bo));
    }

    /**
     * 修改知识库信息
     */
    @SaCheckPermission("knowledge:info:edit")
    @Log(title = "知识库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeInfoBo bo) {
        return toAjax(knowledgeInfoService.updateByBo(bo));
    }

    /**
     * 删除知识库信息
     *
     * @param knowledgeIds 知识库ID串
     */
    @SaCheckPermission("knowledge:info:remove")
    @Log(title = "知识库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{knowledgeIds}")
    public R<Void> remove(@PathVariable Long[] knowledgeIds) {
        return toAjax(knowledgeInfoService.deleteWithValidByIds(List.of(knowledgeIds), true));
    }

    /**
     * 根据用户ID查询知识库信息列表
     *
     * @param userId 用户ID
     */
    @SaCheckPermission("knowledge:info:query")
    @GetMapping("/user/{userId}")
    public R<List<KnowledgeInfoVo>> getByUserId(@PathVariable Long userId) {
        return R.ok(knowledgeInfoService.queryListByUserId(userId));
    }

    /**
     * 根据知识库ID查询知识库信息
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:info:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<KnowledgeInfoVo> getByKnowledgeId(@PathVariable Long knowledgeId) {
        return R.ok(knowledgeInfoService.queryByKnowledgeId(knowledgeId));
    }

    /**
     * 上传文档到知识库
     *
     * @param knowledgeId 知识库ID
     * @param file        上传的文件
     */
    @SaCheckPermission("knowledge:info:upload")
    @Log(title = "知识库文档上传", businessType = BusinessType.INSERT)
    @PostMapping("/{knowledgeId}/upload")
    public R<String> uploadDocument(@PathVariable String knowledgeId,
                                    @RequestParam("file") MultipartFile file) {
        return R.ok(knowledgeInfoService.uploadDocument(knowledgeId, file));
    }

    /**
     * 批量上传文档到知识库
     *
     * @param knowledgeId 知识库ID
     * @param files       上传的文件列表
     */
    @SaCheckPermission("knowledge:info:upload")
    @Log(title = "知识库批量文档上传", businessType = BusinessType.INSERT)
    @PostMapping("/{knowledgeId}/batch-upload")
    public R<List<String>> batchUploadDocuments(@PathVariable String knowledgeId,
                                                @RequestParam("files") MultipartFile[] files) {
        return R.ok(knowledgeInfoService.batchUploadDocuments(knowledgeId, files));
    }

    /**
     * 检索知识库
     *
     * @param knowledgeId 知识库ID
     * @param query       查询文本
     * @param limit       返回结果数量限制
     */
    @SaCheckPermission("knowledge:info:search")
    @PostMapping("/{knowledgeId}/search")
    public R<List<KnowledgeSearchResultVo>> searchKnowledge(@PathVariable String knowledgeId,
                                                            @RequestParam String query,
                                                            @RequestParam(defaultValue = "10") Integer limit) {
        return R.ok(knowledgeInfoService.searchKnowledge(knowledgeId, query, limit));
    }

    /**
     * 获取知识库处理状态
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:info:query")
    @GetMapping("/{knowledgeId}/status")
    public R<KnowledgeStatusVo> getKnowledgeStatus(@PathVariable String knowledgeId) {
        return R.ok(knowledgeInfoService.getKnowledgeStatus(knowledgeId));
    }
}